var express = require('express')
const app = express()
const path = require('path')
const fs = require('fs')
const bodyParser = require('body-parser')
const router = require('./router/index')
const redisClient = require('./redis')
const getCredential = require('./oss')
const getAuth = require('./alipay')

redisClient.set('foo', 'bar')
redisClient.hmset("key1:key2", "foo", "bar", "hello", "world");
redisClient.hmset("key", "name", "孙继红");
redisClient.hmset("key", "name", "孙继红");
redisClient.hset("ff:aa:vv", "name", "孙继红111")
redisClient.hgetall("key", function(err, value) {
    console.log(value.foo); // > "bar"
    console.log(value.hello); // > "world"
  });
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use('/', express.static(__dirname));
app.all('*', function(req, res, next) {
    console.log(req.headers.origin)
    res.header("Access-Control-Allow-Origin", req.headers.origin);
    
    res.header("Access-Control-Allow-Credentials", true);
    // res.header("Access-Control-Allow-Headers", "Authorization,Origin, X-Requested-With, Content-Type, Accept");
    res.header("Access-Control-Allow-Headers", "content-type,token,*");
    res.header("Access-Control-Allow-Methods", "*");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    if(req.method === 'OPTIONS') {
        res.sendStatus(200)
    } else {
        next();
    }
});



app.get('/', function(req, res){
    res.sendFile(__dirname+'/index.html')
})
app.get('/testmp3', function(req, res){
    res.header("Content-Type",'audio/mpeg')
    res.header('Content-Disposition', 'attachment;filename=a.mp3') // 设置下载名称
    res.sendFile(__dirname+'/我的一个道姑朋友.mp3')
})
app.get('/testpdf', function(req, res){
    res.header("Content-Type",'application/octet-stream')
    // res.header('Content-Disposition', 'attachment;filename=理赔须知.pdf') // 设置下载名称
    res.sendFile(__dirname+'/理赔须知.pdf')
})
router(app)
app.get('/index/bootstrap', function(req, res){
    var file = path.join(__dirname, '/JSON/index-bootstrap.json'); //文件路径，__dirname为当前运行js文件的目录
	
	//读取json文件
    fs.readFile(file, 'utf-8', function(err, data) {
        if (err) {
            res.send('文件读取失败');
        } else {
            res.send(data);
        }
    });
})
app.get('/oss-token', function(req, res, next){
    console.log('111112')
    getCredential(req, res, (data)=>{
        console.log('112', data)
        res.send({
            code: 200,
            data: data
        })
    })
})
app.get('/get_alipay_auth', function(req, res, next){
    if(!req.query.code){
        res.send({
            code: 200,
            data: '缺少授权code'
        })
        return
    }
    getAuth(req, res, (data)=>{
        res.send({
            code: 200,
            data: data
        })
    })
})
app.get('/PingFang-Regula', function(req, res) {
    res.header("Content-Type", "application/octet-stream");
    res.sendFile(path.join(__dirname, '/PingFang-Bold.ttf'))
})


app.listen(3000, function(){
    console.log('服务器在3000端口启动')
})